package org.elephas.webapp.model;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.elephas.model.Blog;
import org.elephas.service.BlogFinder;

import salve.depend.Dependency;

/**
 * A loadable detachable {@link IModel} for {@link Blog}s.
 * 
 * @author Gerolf Seitz
 * 
 */
public class DetachableBlogModel extends LoadableDetachableModel<Blog> {
	private static final long serialVersionUID = 1L;

	private String name;

	@Dependency
	private BlogFinder finder;

	public DetachableBlogModel() {
	}

	public DetachableBlogModel(Blog blog) {
		name = blog.getName();
	}

	public DetachableBlogModel(String blogName) {
		name = blogName;
	}

	@Override
	protected Blog load() {
		if (name == null) {
			return new Blog();
		}
		return finder.findByName(name);
	}
}
